<?php
    if (!defined('EXPONENT')) exit('');
    if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate"),$loc))
    {
        global $user;
        $id=0;
        if (isset($_REQUEST['id']))
            $id=intval($_REQUEST['id']);
        $template=new template("mailsystemmodule","_mails_manage_edit",$loc);               if(isset($_REQUEST['mail_status']))
        {
            if($_REQUEST['mail_status']==0)
            {
                $table='mailsystem_mail';
            }
            else if($_REQUEST['mail_status']==1)
            {
                $table='mailsystem_mail_sent';
            }
            else
            {
                $table='mailsystem_mail_error';
            }
            
        }
        $content=$db->selectObject($table,'id='.$id);
        $group=$db->selectDropdown('mailsystem_template','group');
        
        
        $defaulcontent=null;
        
        //For group
        $groups=null;
        foreach($group as $k=>$v) 
        {
            $groups[$v]=$v;            
        }   
        //$content->group=$group;
        if (!$content->id)
        {
            $content->tmp_id='tmp_'.rand(100000,999999);
            $temp=$db->selectObject('mailsystem_template','id='.$_REQUEST['default_temp']);
            $content->subject='';
            $content->content=$temp->content;
            $content->mail_type=0;            
            $content->file='';
            $content->mail_status=0;            
            $content->email_from=$user->email;
            if($user->firstname==""&&$user->lastname=="")
                $content->email_from_name=$user->username;
            else
                $content->email_from_name=$user->lastname." ".$user->firstname;           
            $content->group=$temp->group;
            $content->mailto='';
        }
        else
        {
            $content->mailto=$content->email_to;
            foreach ($group as $k=>$v)
            {
                if (trim($v)!="")
                {
                    $group[$v]=$v;
                }
                if ($content->group == $v)
                    $content->group_default = $v;
            }
        }
        $_mail_type = array(
            0=>'HTML',
            1=>'Text',
        );
        
        $form = new form();
        //for_new mail
        if($content->tmp_id)
        $form->meta('tmp_id',$content->tmp_id);
        $form->meta('id',$content->id?$content->id:0);
        $form->meta('module','mailsystemmodule');
        $form->meta('action','mails_manage_save');   
        $form->meta('ajax_action','1');  
        
        $form->register('_mail_email_from','From',new textcontrol($user->email,0,true,0,"", 'email'));
        $form->register('_mail_email_from_name','From name',new textcontrol($content->email_from_name,0,false,0,"", true));
        $form->register('_mail_email_to','To',new textcontrol($content->mailto,0,false,0,"", 'email'));
        $form->register('_mail_type','Type',new  dropdowncontrol($content->mail_type,$_mail_type));
        $form->register('_mail_group','Group',new  dropdowncontrol($content->group?$content->group:'default',$groups));
        $form->register('_mail_subject','Subject',new textcontrol($content->subject,0,false,0,"", true));
        /*$html='<a href="#" id="_mail_addAttachment">+ Upload File </a><div id="files_acttach_list"></div>'; 
        $form->register('_mail_addAttachment','_mail_addAttachment',new htmlcontrol($html));*/
        $form->register('_mail_content','Content',new htmleditorcontrol($content->content)); 
        $form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
              
        $template->assign('html_form',$form->toHTML());
    
        $template->assign('mail',$content);
        $template->register_permissions(array('administrate',"configure"),$loc);
        $template->output();
    }
    else
    {
        exponent_http_error(403);
    }
?>
